package org.farng.mp3.lyrics3;

import java.util.Iterator;
import java.util.NoSuchElementException;


/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.3 $
 */
public class Lyrics3v1Iterator implements Iterator {
    /** DOCUMENT ME! */
    private Lyrics3v1 tag = null;

    /** DOCUMENT ME! */
    private int lastIndex = 0;

    /** DOCUMENT ME! */
    private int removeIndex = 0;

    /**
     * Creates a new Lyrics3v1Iterator object.
     *
     * @param lyrics3v1Tag DOCUMENT ME!
     */
    public Lyrics3v1Iterator(Lyrics3v1 lyrics3v1Tag) {
        tag = lyrics3v1Tag;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean hasNext() {
        return !((tag.getLyric().indexOf('\n', lastIndex) < 0) && (lastIndex > tag.getLyric().length()));
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws NoSuchElementException DOCUMENT ME!
     */
    public Object next() {
        int nextIndex = tag.getLyric().indexOf('\n', lastIndex);

        removeIndex = lastIndex;

        String line = null;

        if (lastIndex >= 0) {
            if (nextIndex >= 0) {
                line = tag.getLyric().substring(lastIndex, nextIndex);
            } else {
                line = tag.getLyric().substring(lastIndex);
            }

            lastIndex = nextIndex;
        } else {
            throw new NoSuchElementException("Iteration has no more elements.");
        }

        return line;
    }

    /**
     * DOCUMENT ME!
     */
    public void remove() {
        String lyric = tag.getLyric().substring(0, removeIndex) + tag.getLyric().substring(lastIndex);
        tag.setLyric(lyric);
    }
}